package ch.cn.hdmidemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaMetadataRetriever;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.easy.carpanel.uart.uartJni;
import com.example.x6.serial.SerialPort;
import com.hisilicon.android.tvapi.HitvManager;
import com.hisilicon.android.tvapi.Picture;
import com.hisilicon.android.tvapi.SourceManager;
import com.hisilicon.android.tvapi.constant.EnumSourceIndex;
import com.hisilicon.android.tvapi.impl.CusExImpl;
import com.hisilicon.android.tvapi.vo.RectInfo;
import com.monkey.miclockview.MiClockView;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private VideoView myVideoView;
    private SourceManager mSourceManager;
    private Picture mPicture;

    public static int width;
    public static int height;
    public static float density;
    public byte[] bytes = new byte[]{0x2e,0x08,0x32,0x73,0x04,0x05};
    public byte[] RequCmd = new byte[]{0x2e,0x08,0x32,0x73,0x04,0x05};
    byte[] read_buf = new byte[1024];
    public boolean startflag=false;     //是否第一次串口通讯
//    private MiClockView miClockView;    //时钟控件
    private int fd;     //用于串口通讯

    private RelativeLayout rl_time;     //码表Layout
    private RelativeLayout rl_clock;     //码表Layout
    private ImageView iv_needle_time;   //码表针控件
    private ImageView iv_clock;
    private ImageView iv_needle_hour;
    private ImageView iv_needle_minute;
    private ImageView iv_needle_second;   //码表针控件
    private ImageView iv_center;   //码表针控件
    private TextView tv_date;

    private TextView tv_max;
    private SeekBar sb1;
    private SeekBar sb2;
    private SeekBar sb3;
    private SeekBar sb4;

    TextView tv_sb1;
    TextView tv_sb2;
    TextView tv_sb3;
    TextView tv_sb4;

    private long loopTime = 200;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    private Thread thread;  //读串口线程

    private float degree = 0;

    private boolean isAdjust;

    LinearLayout ll_sb;

    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        Log.e("ma===onCreate", getRequestedOrientation()+"===");

//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

//        if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
//        }else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
//        }
        //setContentView(R.layout.activity_main2);
        setContentView(R.layout.activity_main_jinguan_310);

        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        density = metrics.density;
        width = metrics.widthPixels;
        height = metrics.heightPixels;
        Log.e("ma===onCreate1", getRequestedOrientation()+"==="+density+"==="+width+"==="+height);


//        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(436, 590);
//        params.setMargins(436, 0, 436, 590);
//        miClockView = new MiClockView(this, R.drawable.clock_bg, R.drawable.needle_second, R.drawable.needle_minute, R.drawable.needle_hour, R.drawable.center);
//        addContentView(miClockView, params);
//        FrameLayout rl_vv = (FrameLayout) findViewById(R.id.rl_vv);
        rl_time = (RelativeLayout) findViewById(R.id.rl_time);
        rl_clock = (RelativeLayout) findViewById(R.id.rl_clock);
        iv_needle_time = (ImageView) findViewById(R.id.iv_needle_time);
        iv_clock = (ImageView) findViewById(R.id.iv_clock);
        iv_needle_hour = (ImageView) findViewById(R.id.iv_needle_hour);
        iv_needle_minute = (ImageView) findViewById(R.id.iv_needle_minute);
        iv_needle_second = (ImageView) findViewById(R.id.iv_needle_second);
        tv_date = (TextView) findViewById(R.id.tv_date);
//		iv_center = (ImageView) findViewById(R.id.iv_center);
//        tv_max = (TextView) findViewById(R.id.tv_max);
        ll_sb = (LinearLayout) findViewById(R.id.ll_sb);
        sb1 = (SeekBar) findViewById(R.id.sb1);
        sb2 = (SeekBar) findViewById(R.id.sb2);
        sb3 = (SeekBar) findViewById(R.id.sb3);
        sb4 = (SeekBar) findViewById(R.id.sb4);
        tv_sb1 = (TextView) findViewById(R.id.tv_sb1);
        tv_sb2 = (TextView) findViewById(R.id.tv_sb2);
        tv_sb3 = (TextView) findViewById(R.id.tv_sb3);
        tv_sb4 = (TextView) findViewById(R.id.tv_sb4);

//        iv_center.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                finish();
//
////                degree+=10;
////
////                iv_needle_time.setPivotX(iv_needle_time.getWidth() / 2);
////                iv_needle_time.setPivotY(iv_needle_time.getHeight() / 2);    //支点在图片中心  22:中心偏移量
////                iv_needle_time.setRotation(90 + degree);
////
////                iv_needle_second.setPivotX(iv_needle_second.getWidth() / 2);
////                iv_needle_second.setPivotY(iv_needle_second.getHeight() / 2);    //支点在图片中心  22:中心偏移量
////                iv_needle_second.setRotation(90 + degree);
//            }
//        });
        iv_needle_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

//                degree+=30;
//
//                iv_needle_time.setPivotX(iv_needle_time.getWidth() / 2);
//                iv_needle_time.setPivotY(iv_needle_time.getHeight() / 2);    //支点在图片中心  22:中心偏移量
//                iv_needle_time.setRotation(90 + degree);  //-120:起始角度

//                rl_time.setVisibility(View.GONE);
//                miClockView.setVisibility(View.GONE);
            }
        });

        iv_clock.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.e("iv_clock===", "==="+event.getAction());

                if(event.getAction()==1){

                    if(!isAdjust){
                        isAdjust = true;
                        ll_sb.setVisibility(View.VISIBLE);
//                        sb1.setVisibility(View.VISIBLE);
//                        sb2.setVisibility(View.VISIBLE);
//                        sb3.setVisibility(View.VISIBLE);
//                        sb4.setVisibility(View.VISIBLE);
                    }else{
                        isAdjust = false;
                        ll_sb.setVisibility(View.GONE);
//                        sb1.setVisibility(View.GONE);
//                        sb2.setVisibility(View.GONE);
//                        sb3.setVisibility(View.GONE);
//                        sb4.setVisibility(View.GONE);
                    }

                }

                return true;
            }
        });

//        miClockView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){////调音监听器
            public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser){
                mPicture.setBrightness(progress);
                tv_sb1.setText(""+progress);

                Log.e("sb1===", "==="+progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){////调音监听器
            public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser){
                mPicture.setContrast(progress);
                tv_sb2.setText(""+progress);

                Log.e("sb2===", "==="+progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){////调音监听器
            public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser){
                mPicture.setSaturation(progress);
                tv_sb3.setText(""+progress);

                Log.e("sb3===", "==="+progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb4.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){////调音监听器
            public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser){
                mPicture.setSharpness(progress);
                tv_sb4.setText(""+progress);

                Log.e("sb4===", "==="+progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        second = calendar.get(Calendar.SECOND);
        minute = calendar.get(Calendar.MINUTE);
        hour = calendar.get(Calendar.HOUR);
        date = calendar.get(Calendar.DATE);
        month = calendar.get(Calendar.MONTH)+1;
        year = calendar.get(Calendar.YEAR);


//        miClockView.setSecond(calendar.get(Calendar.SECOND));
//        miClockView.setMinute(calendar.get(Calendar.MINUTE));
//        miClockView.setHour(calendar.get(Calendar.HOUR));
//        miClockView.setDate(calendar.get(Calendar.DATE));
//        miClockView.setMonth(calendar.get(Calendar.MONTH)+1);
//        miClockView.setYear(calendar.get(Calendar.YEAR));


//        Intent intent=new Intent();
////        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//        intent.setComponent(new ComponentName("cn.qimate.bike", "cn.qimate.bike.full.SplashActivity"));
////        com.autonavi.amapauto===com.autonavi.auto.remote.fill.UsbFillActivity
//
//        mgr = new LocalActivityManager(this, false);
//        mgr.dispatchCreate(savedInstanceState);
//
//        FrameLayout mContainer = (FrameLayout) findViewById(R.id.ll_act);
//        mContainer.addView((mgr.startActivity("MyOtherActivityInstance1", new Intent(this, gActivity.class))).getDecorView());


//        intent = getPackageManager().getLaunchIntentForPackage("cn.qimate.bike");   //跳转到高德地图车机版
//        startActivity(intent);

        m_myHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                CusExImpl.getInstance().cus_setGpioVal(15, 0);
                mSourceManager = HitvManager.getInstance().getSourceManager();
                mPicture = HitvManager.getInstance().getPicture();
                initHdmi(448,  0,  1024,  590);
                CusExImpl.getInstance().cus_setGpioVal(15, 1);
            }
        }, 4 * 1000);

        init_serial();
        isThread = true;
        loopRead();

//        MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
//        metadataRetriever.setDataSource(file_path);

//        AnimationDrawable animationDrawable1 = new AnimationDrawable();
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_1 ),200);
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_2 ),200);
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_3 ),200);
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_4 ),200);
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_5 ),200);
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_6 ),200);
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_7 ),200);
//        animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_8 ),200);
//        animationDrawable1.setOneShot(true);
//        imageView_2.setImageDrawable(animationDrawable1);
//        animationDrawable1.start();

//        try {
//            FFmpegFrameGrabber ff = new FFmpegFrameGrabber(Environment.getExternalStorageDirectory()+ File.separator + "1/yy.mp4");
//            ff.start();
//            int lenght = ff.getLengthInFrames();
//
//            Log.e("ff===", lenght+"==="+ff.getFrameRate());
//
////            System.out.println(lenght+"==="+ff.getFrameRate());
//        } catch (Exception e) {
//            Log.e("ff===e", "==="+e);
//            e.printStackTrace();
//        }

    }

    LocalActivityManager mgr;
    @Override
    protected void onResume() {
        super.onResume();
//        mgr.dispatchResume();


//        mContainer.addView((mgr.startActivity("MyOtherActivityInstance1", getPackageManager().getLaunchIntentForPackage("cn.qimate.bike"))).getDecorView());
//        mContainer.addView((mgr.startActivity("MyOtherActivityInstance1", intent)).getDecorView());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSourceManager.deselectSource(EnumSourceIndex.SOURCE_HDMI3, true);
        mSourceManager.selectSource(EnumSourceIndex.SOURCE_MEDIA, 0);
        if (myVideoView != null) {
            myVideoView.suspend();
        }
    }
//    public void onConfigurationChanged(Configuration newConfig) {
//        Log.e("onConfigurationChang=0", newConfig.orientation+"===");
//
//        if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//            Log.e("onConfigurationChang=1", getRequestedOrientation()+"===");
//        } else {
//            Log.e("onConfigurationChang=2", getRequestedOrientation()+"===");
//        }
//    }
    protected void send(){
        if(!startflag){
            startflag=true;
            write(RequCmd);
            loopTime=50;
        }else if(startflag){
            startflag=false;
            read(bytes); //TODO 串口获取车速
            loopTime=200;
        }
    }

    int n=0;
    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {

                while (isThread){
                    m_myHandler.sendEmptyMessage(1);

                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    n++;

                    if(n%5==0){
                        n=0;
                        m_myHandler.sendEmptyMessage(2);
                    }

                }
            }
        });
        thread.start();
    }

    Calendar calendar = Calendar.getInstance();
    int second;
    int minute;
    int hour;
    int date;
    int month;
    int year;

    /* 时针角度 */
    private float mHourDegree;
    /* 分针角度 */
    private float mMinuteDegree;
    /* 秒针角度 */
    private float mSecondDegree;


    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    rl_time.setVisibility(View.GONE);
                    rl_clock.setVisibility(View.GONE);
                    break;
                case 1:
                   // send();
                    read(bytes); //TODO 串口获取车速
//                    loopTime=200;

//                    Log.e("m_myHandler===1", "==="+n);
//
//                    if(n==50){
////                        miClockView.setMinute(59);
////                        miClockView.setHour(23);
////                        miClockView.setDate(31);
////                        miClockView.setMonth(12);
////                        miClockView.setYear(2020);
//
//                        second = 50;
//                        minute = 59;
//                        hour = 23;
//                        date = 31;
//                        month = 12;
//                        year = 2020;
//                    }

                    break;

                case 2:
//                    int second = miClockView.getSecond();
                    second++;
//                    miClockView.setSecond(second);

//                    int minute = miClockView.getMinute();
//                    int hour = miClockView.getHour();
//                    int date = miClockView.getDate();
//                    int month = miClockView.getMonth();
//                    int year = miClockView.getYear();

                    if(second%60==0){
                        second = 0;
//                        miClockView.setSecond(0);
                        minute++;
                    }

                    if(minute>0 && minute%60==0){
                        minute = 0;
//                        miClockView.setMinute(0);
                        hour++;
                    }

                    if(hour>0 && hour%24==0){
                        hour = 0;
//                        miClockView.setHour(0);

                        calendar.set(year, month, date, hour, minute, second);
                        calendar.add(Calendar.DATE, 1);

//                        Date cdate = calendar.getTime();
//                        System.out.println(cdate.ge);


                        date = calendar.get(Calendar.DATE);
                        month = calendar.get(Calendar.MONTH);
                        year = calendar.get(Calendar.YEAR);

//                        miClockView.setDate(date);
//                        miClockView.setMonth(month);
//                        miClockView.setYear(year);

//                        date++;
                    }

//                    Date date = new Date();
//                    Calendar calendar = new GregorianCalendar();
//                    calendar.setTime(date);

                    mSecondDegree = -90 + second / 60f * 360;
                    mMinuteDegree = -90 + (minute+ second / 60f) / 60f * 360;
                    mHourDegree = -90 + (hour+minute / 60f) / 12f * 360;

                    iv_needle_second.setPivotX(iv_needle_second.getWidth() / 2);
                    iv_needle_second.setPivotY(iv_needle_second.getHeight() / 2);    //支点在图片中心  22:中心偏移量
                    iv_needle_second.setRotation(mSecondDegree);

                    iv_needle_minute.setPivotX(iv_needle_minute.getWidth() / 2);
                    iv_needle_minute.setPivotY(iv_needle_minute.getHeight() / 2);    //支点在图片中心  22:中心偏移量
                    iv_needle_minute.setRotation(mMinuteDegree);

                    iv_needle_hour.setPivotX(iv_needle_hour.getWidth() / 2);
                    iv_needle_hour.setPivotY(iv_needle_hour.getHeight() / 2);    //支点在图片中心  22:中心偏移量
                    iv_needle_hour.setRotation(mHourDegree);


                    Log.e("m_myHandler===", year+"==="+month+"==="+date+"==="+hour+"==="+minute+"==="+second);

//                    miClockView.setSecond(second);
//                    miClockView.setMinute(minute);
//                    miClockView.setHour(hour);

//                    miClockView.setMydate((year< 10 ? ("0" + year) : year) + "-" + (month < 10 ? ("0" + month) : month) + "-" + (date < 10 ? ("0" + date) : date));

                    tv_date.setText((year< 10 ? ("0" + year) : year) + "-" + (month < 10 ? ("0" + month) : month) + "-" + (date < 10 ? ("0" + date) : date));


//                    miClockView.setHour(read_buf[6]);
//                    miClockView.setMinute(read_buf[7]);
//                    miClockView.setMydate("20" + read_buf[3] + "-" + (read_buf[4] < 10 ? ("0" + read_buf[4]) : read_buf[4]) + "-" + (read_buf[5] < 10 ? ("0" + read_buf[5]) : read_buf[5]));

                    break;



                default:
                    break;
            }
            return false;
        }
    });
    /* 打开串口 */
    private void init_serial() {
        try {
            fd = uartJni.open("/dev/ttyAMA2", 115200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /* 串口发送字节 */
    public void write(byte[] bytes){
        try {
            uartJni.write(fd, bytes, bytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String toHex(byte[] buffer,int read_count) {
        StringBuffer sb = new StringBuffer(read_count * 2);
        for (int i = 0; i < read_count; i++) {
            sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));
            sb.append(Character.forDigit(buffer[i] & 15, 16));
        }
        return sb.toString();
    }
    public static byte charToByte(char c) {
        byte b = (byte) "0123456789abcdef".indexOf(c);
        return b;
    }
    public static byte[] hexStringToBytes(String hex) {
        while (hex.length() < 2)
            return null;
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (charToByte(achar[pos]) << 4 | charToByte(achar[pos + 1]));
        }
        return result;
    }
    /* 串口发送字节 */
    public int read(byte[] bytes){
        try {
             read_buf = new byte[1024];
            int read_count = uartJni.read(fd, read_buf, read_buf.length);
            if (read_count < 0) {
                read_count = 0;
            }

            String tmpstr = toHex(read_buf, read_count).toString();
            if (!tmpstr.equals("")) {
                int index;
                index = tmpstr.indexOf("2e");
                int cmdcnt  =Integer.parseInt(tmpstr.substring(index+4, index +6));
                String cmd= tmpstr.substring(index, index + (cmdcnt+4)*2);
                read_buf =  hexStringToBytes(cmd);
                String value = CarUtil.toHex(read_buf, (cmdcnt+4));
                Log.e("jiangyunwei=5", index+"==="+cmd+"==="+value);

                if(read_buf[0]==0x2E){
                    switch(read_buf[1]){
                        case  0x08:
                            if(read_count>=10){
                                Log.e("jiangyunwei=0x09", read_buf[0] + "===" + read_buf[1] + "===" + read_buf[2] + "===" + read_buf[3] + "===" + read_buf[4] + "===" + read_buf[5] + "===" + read_buf[6] + "===" + read_buf[7] + "===" + value);

                                second = 0;
                                minute = read_buf[7];
                                hour = read_buf[6];
                                date = read_buf[5];
                                month = read_buf[4];
                                year = Integer.parseInt("20" +read_buf[3]);

                                tv_date.setText("20" + read_buf[3] + "-" + (read_buf[4] < 10 ? ("0" + read_buf[4]) : read_buf[4]) + "-" + (read_buf[5] < 10 ? ("0" + read_buf[5]) : read_buf[5]));


//                                miClockView.setSecond(0);
//                                miClockView.setMinute(read_buf[7]);
//                                miClockView.setHour(read_buf[6]);
//                                miClockView.setDate(read_buf[5]);
//                                miClockView.setMonth(read_buf[4]);
//                                miClockView.setYear(Integer.parseInt("20" +read_buf[3]));
//                                miClockView.setMydate("20" + read_buf[3] + "-" + (read_buf[4] < 10 ? ("0" + read_buf[4]) : read_buf[4]) + "-" + (read_buf[5] < 10 ? ("0" + read_buf[5]) : read_buf[5]));
                            }
                            break;

                        case  0x26:
                            if(read_count>=10) {
                                Log.e("jiangyunwei=0x09", read_buf[0] + "===" + read_buf[1] + "===" + read_buf[2] + "===" + read_buf[3] + "===" + read_buf[4] + "===" + value);
                                int degree = read_buf[3];
                                int degree2 = read_buf[4];
                                iv_needle_time.setPivotX(iv_needle_time.getWidth() / 2);
                                iv_needle_time.setPivotY(iv_needle_time.getHeight() / 2);    //支点在图片中心  22:中心偏移量

                                if(degree * 16 + degree2 >= 210){
                                    iv_needle_time.setRotation(300);  //-120:起始角度
                                }else{
                                    iv_needle_time.setRotation(90 + degree * 16 + degree2);  //-120:起始角度
                                }


                            }
                            break;

                        case 0x09:
                            if (read_count >=5) {
                                Log.e("jiangyunwei=开关", rl_time+ "===" +rl_clock+ "===" +read_buf[0] + "===" + read_buf[1] + "===" + read_buf[2] + "===" + read_buf[3] + "===" + value);

                                m_myHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (read_buf[3] == 1) {
                                            rl_time.setVisibility(View.VISIBLE);
                                            rl_clock.setVisibility(View.VISIBLE);
                                        } else {
                                            rl_time.setVisibility(View.GONE);
                                            rl_clock.setVisibility(View.GONE);
                                        }
                                    }
                                });

                            }
                            break;

                    }

                }
            }
        } catch (Exception e) {
            Log.e("f2===e", e+"===");

            e.printStackTrace();
        }
        return 1;
    }
    private void initHdmi(final int hlocationX, final int hlocationY, final int width, final int height) {
        myVideoView = findViewById(R.id.mvideoview);
//        myVideoView.setRotation(180);
//        Util_Rotate3DAnimation rotation2 = new Util_Rotate3DAnimation(myVideoView, width, 0, 0, 0, 0, 0, 0.0f, false, false);
//        rotation2.setDuration(1000);
//        rotation2.setFillAfter(true);
//        rotation2.setInterpolator(new AccelerateInterpolator());
//        myVideoView.startAnimation(rotation2);

//        TvCommonManager.getInstance().setInputSource(TvOsType.EnumInputSource.E_INPUT_SOURCE_HDMI);   //TODO
//        TvPictureManager.getInstance().setVideoItem(TvPictureManager.PICTURE_SHARPNESS, 0);

        myVideoView.post(new Runnable() {
            @Override
            public void run() {
                int location[] = new int[2];
                myVideoView.getLocationOnScreen(location);
                Log.e(TAG, "initHdmi: myVideoView.getX() = " + location[0]);
                Log.e(TAG, "initHdmi: myVideoView.getY() = " + location[1]);
                Log.e(TAG, "initHdmi: myVideoView.getWidth() = " + myVideoView.getWidth());
                Log.e(TAG, "initHdmi: myVideoView.getHeight() = " + myVideoView.getHeight());
                reviseXyForHdmi(location[0] ,location[1], myVideoView.getWidth(), myVideoView.getHeight());
//                reviseXyForHdmi(hlocationX ,hlocationY, width, height, 0);
            }
        });
        if (true) {
            mSourceManager.deselectSource(EnumSourceIndex.SOURCE_MEDIA, true);
            mSourceManager.selectSource(EnumSourceIndex.SOURCE_HDMI3, 0);

            mPicture.setAspect(4, true);
            mPicture.setBrightness(30);
            mPicture.setContrast(40);
            mPicture.setSaturation(55);
            mPicture.setSharpness(10);
            setAppScreenBrightness(100);

            tv_sb1.setText(""+30);
            tv_sb2.setText(""+40);
            tv_sb3.setText(""+55);
            tv_sb4.setText(""+10);
        }
    }
    /** * 2.设置 APP界面屏幕亮度值方法 * **/
    private void setAppScreenBrightness(int birghtessValue) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
    lp.screenBrightness = birghtessValue / 255.0f;
    window.setAttributes(lp);
    }
    private boolean reviseXyForHdmi(int locationX, int locationY, int width, int height) {
//        int x = locationX;
//        int y = locationY;
//        int w = width;
//        int h = height;
//
//        switch (angle) {
//            case 0:
//                break;
//            case 90:
//                locationX = 1920 - (y + h);
//                locationY = x;
//                width = h;
//                height = w;
//                break;
//            case 180:
//                locationX = 1920 - (x + w);
//                locationY = 1080 - (y + h);
////                locationY = 590;
//                break;
//            case 270:
//                locationX = y;
//                locationY = 1080 - (x + w);
//                width = h;
//                height = w;
//                break;
//            default:
//                return false;
//        }
        Log.e(TAG, "reviseXyForHdmi: locationX = " + locationX);
        Log.e(TAG, "reviseXyForHdmi: locationY = " + locationY);
        Log.e(TAG, "reviseXyForHdmi: width = " + width);
        Log.e(TAG, "reviseXyForHdmi: height = " + height);
        mSourceManager.setWindowRect(new SmallScreenRectInfo(locationX, locationY, width, height), 0);
//        mSourceManager.setDisplayOnLeft(true, 0);
        return true;
    }

    class SmallScreenRectInfo extends RectInfo {
        public SmallScreenRectInfo(int x, int y, int width, int height) {
            setX(x);
            setY(y);
            setW(width);
            setH(height);
        }
    }
}